using HZY.Framework.Repository.EntityFramework.ApplicationServices;

namespace HZY.Shared.Admin.ApplicationServices;

/// <summary>
/// 服务父类
/// </summary>
/// <typeparam name="TEntity">数据库实体</typeparam>
/// <typeparam name="TKey">数据库实体主键类型</typeparam>
/// <typeparam name="TSearchDto">查询检索Dto</typeparam>
/// <typeparam name="TSaveFormDto">表单保存Dto</typeparam>
public abstract class ApplicationService<TEntity, TKey, TSearchDto, TSaveFormDto>(IServiceProvider serviceProvider)
    : ApplicationService<TEntity>(serviceProvider)
    where TEntity : class, new()
    where TSearchDto : class, new()
    where TSaveFormDto : class, new()
{
    /// <summary>
    /// 获取列表数据
    /// </summary>
    /// <param name="pagingSearchInput"></param>
    /// <returns></returns>
    public virtual async Task<PagingView> FindListAsync(PagingSearchInput<TSearchDto> pagingSearchInput)
    {
        var query = Repository.SelectNoTracking;

        var result = await Repository.AsPagingViewAsync(query, pagingSearchInput);
        // //覆盖值
        // result
        //     .FormatValue(query, w => w.CreationTime, (oldValue) => oldValue.ToString("yyyy-MM-dd"))
        //     .FormatValue(query, w => w.LastModificationTime, (oldValue) => oldValue?.ToString("yyyy-MM-dd"))
        //     ;
        return result;
    }

    /// <summary>
    /// 根据id数组删除
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public virtual Task DeleteListAsync(List<TKey> ids)
    {
        return Repository.DeleteByIdsAsync(ids);
    }

    /// <summary>
    /// 查询表单数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual async Task<Dictionary<string, object?>> FindFormAsync(TKey id)
    {
        var res = new Dictionary<string, object?>();
        var form = await Repository.FindByIdAsync(id);
        form = form.NullSafe();

        if (id is Guid newId)
        {
            res[nameof(id)] = newId == Guid.Empty ? "" : newId;
        }
        else if (id is int newIntId)
        {
            res[nameof(id)] = newIntId == 0 ? "" : newIntId;
        }
        else if (id is long newLongId)
        {
            res[nameof(id)] = newLongId == 0 ? "" : newLongId;
        }
        else
        {
            res[nameof(id)] = id;
        }

        res[nameof(form)] = form;
        return res;
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    public virtual Task SaveFormAsync(TSaveFormDto form)
    {
        return Repository.InsertOrUpdateAsync(form as TEntity ?? throw MessageBox.Show("保存函数参数Dto不是数据库类型，请对保存函数重写！"));
    }

    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="pagingSearchInput"></param>
    /// <returns></returns>
    public virtual async Task<byte[]> ExportExcelAsync(PagingSearchInput<TSearchDto> pagingSearchInput)
    {
        pagingSearchInput.Page = -1;
        var tableViewModel = await FindListAsync(pagingSearchInput);
        return ExcelUtil.ExportExcelByPagingView(tableViewModel, null, "Id");
    }
}